package com.easyCms.base.busi;

import com.easyCms.base.busi.vo.BsNavVO;
import com.easyCms.base.busi.vo.BsNavVO.ChildrenNavVO;
import com.easyCms.base.entity.BsNav;
import com.easyCms.base.entity.CrmUserAuthNav;
import com.easyCms.base.service.BsNavSV;
import com.easyCms.base.service.CrmUserAuthNavSV;
import com.easyCms.base.web.AdminFrameDataController;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Transactional
public class BsNavBusi {
    private static Logger logger = LoggerFactory.getLogger(AdminFrameDataController.class);
    @Autowired
    private BsNavSV bsNavSV;
    @Autowired
    private CrmUserAuthNavSV crmUserAuthNavSV;

    public List<BsNavVO> initNav(int authType) {
        Map<Long, String> navMap = getNavMap(authType);

        List<BsNav> bsNavList = bsNavSV.findValidParentsNav();// 父级

        List<BsNavVO> bsNavVOs = new ArrayList<BsNavVO>();
        for (BsNav nav : bsNavList) {
            if (StringUtils.isBlank(navMap.get(nav.getId()))) {
                continue;
            }
            List<ChildrenNavVO> childrens = new ArrayList<ChildrenNavVO>();
            BsNavVO bsNavVO = new BsNavVO();
            bsNavVO.setTitle(nav.getTitle());
            bsNavVO.setIcon(nav.getIcon());
            bsNavVO.setSpread(nav.getSpread() == 1 ? true : false);
            bsNavVO.setHref(nav.getHref());
            bsNavVO.setId(nav.getId());
            List<BsNav> childNav = bsNavSV.findChildNav(nav.getId()); // 子级;
            for (BsNav child : childNav) {
                if (StringUtils.isBlank(navMap.get(child.getId()))) {
                    continue;
                }
                ChildrenNavVO childrenNav = new ChildrenNavVO();
                childrenNav.setTitle(child.getTitle());
                childrenNav.setIcon(child.getIcon());
                childrenNav.setSpread(child.getSpread() == 1 ? true : false);
                childrenNav.setHref(child.getHref());
                childrenNav.setId(child.getId());
                childrens.add(childrenNav);
            }
            bsNavVO.setChildren(childrens);
            bsNavVOs.add(bsNavVO);
        }
        return bsNavVOs;
    }

    public Map<Long, String> getNavMap(int authType) {
        Map<Long, String> navMap = new HashMap<Long, String>();
        CrmUserAuthNav authNav = crmUserAuthNavSV.findByAuthType(authType);
        if (authNav != null) {
            String navIds = authNav.getNavIds();
            String[] navArray = navIds.split(",");
            for (int i = 0; i < navArray.length; i++) {
                navMap.put(Long.valueOf(navArray[i]), navArray[i]);
            }
        }
        return navMap;
    }

}
